package com.demo.mqtt.zigbee.handeler.upload;

import com.demo.mqtt.common.client.MqttConnectionUtils;
import com.demo.mqtt.common.constants.TopictConstants;
import com.demo.mqtt.common.utils.AssistUtil;
import com.demo.mqtt.common.utils.HexConvertUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @program: mqtt
 * @description: 心跳上传
 * @author: liuYunKai
 * @create: 2021-03-25 11:59
 */
@Slf4j
@Component
public class HeartBeatUpload implements TopictConstants {
    @Autowired
    private MqttConnectionUtils mqttConnectionUtils;


    public boolean annalyze(String topic, String hesStr, String command) {
//        if(!"02".equalsIgnoreCase(command)){
//            return false;
//        }

        //数据内容有可能不定长，所以直接采用截取的方式
        String contextStr = hesStr.substring(32, hesStr.length() - 4);

        //按字节解析
        ByteBuf byteBuf = Unpooled.buffer();
        byteBuf.writeBytes(HexConvertUtil.hexStrToByteArrs(contextStr));

        //数值
        String value = AssistUtil.readByteBuf(byteBuf, 1);

        if ("ff".equalsIgnoreCase(value)) {
            log.info("心跳正常");
        } else {
            log.info("心跳异常");
        }


        try {
            mqttConnectionUtils.pub("aes_mqtt","msgmsgmsg");
        } catch (MqttException e) {
            e.printStackTrace();
        }

        return true;
    }


}
